<?php
    use User\Model\MeetingModel;
?>
<!-- page heading start-->
<div class="page-heading">
    <h3>
        报名列表
    </h3>

</div>
<link rel="stylesheet" href="/Public/projects/meeting/css/ydui.css"/>
<script type="text/javascript" src="/Public/js/laydatenew/laydate.js"></script>
<!-- page heading end-->
<div class="row">
    <div class="col-sm-12">
        <section class="panel">
            <header class="panel-heading">
                <button class="btn btn-default" data-toggle="modal" data-target="#myModal">增加</button>
                <form method="get" action="/User/Meeting/register" enctype="multipart/form-data" class="form-horizontal">
                    <input type="hidden" name="meetId" value="<?php echo I('get.meetId', '', '');?>">
                    <?php
                    foreach($conditions as $cond_key => $condition){
                            echo "<div class='form-group'><label class='control-label col-sm-2'>";
                            echo $condition['name'] . "</label><div class='col-sm-4'>";
                            $query = "get.query_value" . ($cond_key + 1);
                            echo "<input type='text' class='form-control' value='" . I($query, '', '') . "' name='query_value" . ($cond_key + 1) . "'>";
                            echo "</div></div>";
                        }
                        echo "<div class='form-group'><label class='control-label col-sm-2'>状态</label><div class='col-sm-4'><select class='form-control' name='status'>";
                        $options = MeetingModel::getStatus();
                        $status = I("get.status", -1, "");
                        echo "<option value='-1'";
                        if($status == -1){
                            echo " selected ";
                        }
                        echo ">所有</option>";
                        foreach($options as $option_status => $option){
                            echo "<option value='" . $option_status . "'";
                            if($option_status == $status){
                                echo " selected ";
                            }
                            echo ">" . $option . "</option>";
                        }
                        echo "</select></div></div>";
                        echo "<button class='btn' style='margin-left: 20%'>查询</button>";
                    ?>
                    <button class="btn btn-success" onclick="return download();">导出</button>
                </form>
            </header>
            <div class="panel-body">
                <div style="width: 100%;overflow:auto;">
                    <table class="table table-hover general-table table-bordered" style="width: <?php echo $count;?>px;">
                        <thead>
                        <tr>
                            <?php foreach($fields as $field){
                            echo "<th style='width:100px;'>" . $field['name'] . "</th>";
                            }?>
                            <th style="width: 100px;">状态</th>
                            <th style="width: 200px;">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach($list as $val){
                        echo "<tr style='word-break:break-all;'>";
                            $user_value = $val['field_value'];
                            $user_value_arr = explode(";", $user_value);
                            $export_user_arr = [];
                            foreach ($user_value_arr as $user_value_arr_value) {
                                $user_value_arr_value_arr = explode("|", $user_value_arr_value);
                                if (!strpos($user_value_arr_value_arr[0], ",")) {
                                    $export_user_arr[$user_value_arr_value_arr[0]] = $user_value_arr_value_arr[1];
                                } else {
                                    $key_type = explode(",", $user_value_arr_value_arr[0]);
                                    $export_user_arr[$key_type[0]] = $user_value_arr_value_arr[1];
                                }
                            }
                            foreach($fields as $field){
                                echo "<td>";
                                if (array_key_exists($field['field_id'], $export_user_arr)) {
                                    switch ($field['type']) {
                                        case MeetingModel::FIELD_MULTIPLE:
                                            $field_value_arr = json_decode($export_user_arr[$field['field_id']]);
                                            if (is_array($field_value_arr)) {
                                                $field_value_arr = implode(",", $field_value_arr);
                                            }
                                            echo $field_value_arr;break;
                                        case MeetingModel::FIELD_PIC:
                                            echo "<img style='width: 100%' src='" . $export_user_arr[$field['field_id']] . "' alt=''>";break;
                                        case MeetingModel::FIELD_VIDEO:
                                            echo "<video style='width: 100%' src='" . $export_user_arr[$field['field_id']] . "' ></video>";break;
                                        default:
                                            echo $export_user_arr[$field['field_id']];
                                    }
                                }
                                echo "</td>";
                            }
                            echo "<td>";
                            echo MeetingModel::getStatus($val['status']);
                            echo "</td>";
                            echo "<td><a class='btn btn-success' href='/User/Meeting/registerDetail?meetId=" . I("get.meetId", -1, '')
                            . "&userId=" . $val['user_id'] . "'>编辑</a><button class='btn btn-danger' style='margin-left: 30px;' onclick='return delete_data("
                            . $val['user_id'] . ");'>删除</button></td>";
                            echo "</tr>";
                        }?>

                        </tbody>
                    </table>
                </div>
                <div class="pages">
                    {$page}
                </div>
            </div>
        </section>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <input type="hidden" value="" id="user_id">
            <div class="modal-body">
                <form method="post" id='form' action="/User/Meeting/registerUpdate" enctype="multipart/form-data">
                    <input type="hidden" name="meetId" value="<?php echo I('get.meetId', '', '');?>">
                    <?php
                    foreach($fields as $field){
                        if($field['type'] == MeetingModel::FIELD_TEXT){
                            echo '<div>' . $field['name'] . ':<input type="text" class="form-control" name="' . $field['field_id'] .'"></div>';
                            }
                            if($field['type'] == MeetingModel::FIELD_TEXTAREA){
                                echo '<div>' . $field['name'] . ':<textarea class="form-control" name="' . $field['field_id'] .'"></textarea></div>';
                            }
                            if($field['type'] == MeetingModel::FIELD_SINGLE){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                if($field['option']){
                                    $options = json_decode($field['option'], true);
                                    foreach($options as $option){
                                        echo '<input type="radio" name="' . $field['field_id'] . '" value="' . $option . '">' . $option;
                                    }
                                }
                                echo '</div>';
                            }
                            if($field['type'] == MeetingModel::FIELD_MULTIPLE){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                if($field['option']){
                                    $options = json_decode($field['option'], true);
                                    foreach($options as $k => $option){
                                        echo '<input type="checkbox" name="' . $field['field_id'] . '[]" value="' . $option . '">' . $option;
                                    }
                                }
                                echo '</div>';
                            }
                            if($field['type'] == MeetingModel::FIELD_PIC){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                echo '<img src="" alt="" style="width: 200px;height: 200px;"><input name="' . $field['field_id'] . '" type="file" onchange="readFile(this);">';
                                echo '</div>';
                            }
                            if($field['type'] == MeetingModel::FIELD_CITY){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                echo '<input class="form-control J_Address" style="width: 400px;" placeholder="请输入所在地区" readonly>';
                                echo '<input type="hidden" class="province" name="province' . $field['field_id'] . '" >';
                                echo '<input type="hidden" class="city" name="city' . $field['field_id'] . '" >';
                                echo '<input type="hidden" class="district" name="district' . $field['field_id'] . '" ></div>';
                            }
                            if($field['type'] == MeetingModel::FIELD_ADDRESS){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                echo '<input class="form-control J_Address" style="width: 400px;" placeholder="请输入所在地区" readonly>';
                                echo '<input type="hidden" class="province" name="province' . $display_field[$field['field_id']] . '" >';
                                echo '<input type="hidden" class="city" name="city' . $display_field[$field['field_id']] . '" >';
                                echo '<input type="hidden" class="district" name="district' . $display_field[$field['field_id']] . '" >';
                                echo '<textarea class="form-control" style="width:400px;margin-top: 10px;" name="address' . $display_field[$field['field_id']] . '"></textarea></div>';
                            }
                            if($field['type'] == MeetingModel::FIELD_DATE){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                echo '<input class="form-control" id="date' . $field['field_id'] . '" style="width: 200px;" name="' . $field['field_id'] . '" >';
                                echo '</div>';
                                echo '<script>laydate.render({
                                        elem: "#date' . $field['field_id'] . '"
                                    });</script>';
                            }
                            if($field['type'] == MeetingModel::FIELD_TIME){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                echo '<input class="form-control" id="time' . $field['field_id'] . '" style="width: 200px;" name="' . $field['field_id'] . '" >';
                                echo '</div>';
                                echo '<script>laydate.render({
                                    elem: "#time' . $field['field_id'] . '",
                                    type: "datetime"
                                });</script>';
                            }
                            if($field['type'] == MeetingModel::FIELD_RANGE){
                                echo '<div><div>' . $field['name'] . ":</div>";
                                echo '<input class="form-control" id="start' . $field['field_id'] . '" style="width: 200px;float: left" name="start' . $field['field_id'] . '" ><span style="float: left">-</span>';
                                echo '<input class="form-control" id="end' . $field['field_id'] . '" style="width: 200px;" name="end' . $field['field_id'] . '" >';
                                echo '</div>';
                                echo '<script>laydate.render({
                                            elem: "#start' . $field['field_id'] . '"
                                        });laydate.render({
                                            elem: "#end' . $field['field_id'] . '"
                                        });</script>';
                            }
                        }
                    ?>
                    <?php if($meet && !empty($meet['hotel_service'])){?>
                    <div style="width: 400px;">
                        <div>是否住宿：<input type="radio" value="0" name="is_hotel" <?php if(empty($user) || empty($user['is_hotel'])){ echo 'checked';}?>>否
                            <input type="radio" value="1" name="is_hotel"  <?php if(!empty($user) && !empty($user['is_hotel'])){ echo 'checked';}?>>是</div>
                        <div>住宿人数：<input type="text" value="<?php echo $user['hotel_person']?>" name="hotel_person" class="form-control"></div>
                        <div>住宿备注：<textarea name="hotel_note" class="form-control"><?php echo $user['hotel_note']?></textarea></div>
                    </div>
                    <?php }if($meet && !empty($meet['eat_service'])){?>
                    <div style="width: 400px;">
                        <div>是否就餐：<input type="radio" value="0" name="is_eat" <?php if(empty($user) || empty($user['is_eat'])){ echo 'checked';}?>>否
                            <input type="radio" value="1" name="is_eat"  <?php if(!empty($user) && !empty($user['is_eat'])){ echo 'checked';}?>>是</div>
                        <div>就餐人数：<input type="text" value="<?php echo $user['eat_person']?>" name="eat_person" class="form-control"></div>
                        <div>就餐备注：<textarea name="eat_note" class="form-control"><?php echo $user['eat_note']?></textarea></div>
                    </div>
                    <?php }if($meet && !empty($meet['bus_service'])){?>
                    <div style="width: 400px;">
                        <div>是否乘车：<input type="radio" value="0" name="is_bus" <?php if(empty($user) || empty($user['is_bus'])){ echo 'checked';}?>>否
                            <input type="radio" value="1" name="is_bus"  <?php if(!empty($user) && !empty($user['is_bus'])){ echo 'checked';}?>>是</div>
                        <div>乘车人数：<input type="text" value="<?php echo $user['bus_person']?>" name="bus_person" class="form-control"></div>
                        <div>乘车备注：<textarea name="bus_note" class="form-control"><?php echo $user['bus_note']?></textarea></div>
                    </div>
                    <?php } ?>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="submit" class="btn btn-primary" >提交更改</button>
                    </div>
                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<script>
    var img_tag = '';
    function readFile(img) {
        img_tag = $(img);
        var file = img.files[0];
        if (!/image\/\w+/.test(file.type)) {
            alert("请确保文件为图像类型");
            return false;
        }
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            img_tag.prev().attr("src", this.result);
        }
    }
</script>
<script type="text/javascript">
    $("#myModal").on('shown.bs.modal', function(){
        document.getElementById("form").reset();
    });
    function delete_data(id){
        $.ajax({
            data:{ userId:id },
            dataType:'json',
            url:'/User/Meeting/registerDelete',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
    function download(){
        var query_value1 = '';
        var query_value2 = '';
        var query_value3= '';
        var $query1 = $("input[name='query_value1']");
        if($query1.length != 0){
            query_value1 = $query1.val();
        }
        var $query2 = $("input[name='query_value2']");
        if($query2.length != 0){
            query_value2 = $query2.val();
        }
        var $query3 = $("input[name='query_value3']");
        if($query3.length != 0){
            query_value3 = $query3.val();
        }
        window.location = "/User/Meeting/download?id=" + "<?php echo I('get.meetId', '', '');?>" + "&query_value1="
            + query_value1 + "&query_value2=" + query_value2 + "&query_value3=" + query_value3;
        return false;
    }
</script>
<script type="text/javascript" src="/Public/projects/meeting/js/ydui.citys.js"></script>
<script type="text/javascript" src="/Public/projects/meeting/js/ydui.js"></script>
<script type="text/javascript">
    !function () {
        $(".J_Address").each(function(){
            var $target = $(this);
            var province = $target.nextAll(".province").val();
            var city = $target.nextAll(".city").val();
            var district = $target.nextAll(".district").val();
            $target.citySelect({
                provance: province,
                city:city,
                area:district
            });

            $target.on('click', function (event) {
                event.stopPropagation();
                $target.citySelect('open');
            });

            $target.on('done.ydui.cityselect', function (ret) {
                $(this).val(ret.provance + ' ' + ret.city + ' ' + ret.area);
                $(this).nextAll(".province").val(ret.provance);
                $(this).nextAll(".city").val(ret.city);
                $(this).nextAll(".district").val(ret.area);
            });
        });
    }();
</script>